package com.woniu.woniuk16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.woniuk16.commens.ComUtil;
import com.woniu.woniuk16.dto.StudentDto;
import com.woniu.woniuk16.dto.StusDto;
import com.woniu.woniuk16.dto.WoniuUserDetails;
import com.woniu.woniuk16.entity.WoniuClass;
import com.woniu.woniuk16.entity.WoniuStudent;
import com.woniu.woniuk16.exception.stu.StudentException;
import com.woniu.woniuk16.mapper.WoniuStudentMapper;
import com.woniu.woniuk16.param.student.AddStudentParam;
import com.woniu.woniuk16.param.student.GetStusParam;
import com.woniu.woniuk16.service.WoniuClassService;
import com.woniu.woniuk16.service.WoniuStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-16
 */
@Service
@Data
public class WoniuStudentServiceImpl extends ServiceImpl<WoniuStudentMapper, WoniuStudent> implements WoniuStudentService {
    @Autowired
    private WoniuClassService woniuClassService;
    @Autowired
    private RedisTemplate<String,Object> rtl;

    @Override
    public StusDto selStudent(GetStusParam getStusParam) {
        QueryWrapper<WoniuStudent> qw = new QueryWrapper<>();
        if (getStusParam.getStudentName()!=null){
            qw.like("student_name",getStusParam.getStudentName());
        }
        if (getStusParam.getStudentSex()!=null){
            qw.eq("student_sex",getStusParam.getStudentSex());
        }
        if (getStusParam.getClassId()!=null){
            qw.eq("class_id",getStusParam.getClassId());
        }
        qw.orderByDesc("student_id");
        Page<WoniuStudent> p = new Page<>();
        p.setCurrent(getStusParam.getPageIndex());
        p.setSize(getStusParam.getPageSize());
        Page<WoniuStudent> page1 = page(p, qw);
        StusDto stusDto = new StusDto();
        stusDto.setStus(ComUtil.copyList(page1.getRecords(), StudentDto.class));
        stusDto.setPageIndex(page1.getCurrent());
        stusDto.setTotal(page1.getTotal());
        return stusDto;
    }

    @Override
    public boolean saveStudent(AddStudentParam addStudentParam) throws StudentException {
        WoniuClass byId = woniuClassService.getById(addStudentParam.getClassId());
        if (byId==null){
            throw new StudentException("添加失败，无学生班级信息");
        }
        WoniuStudent student = ComUtil.copyProperties(addStudentParam, WoniuStudent.class);
        WoniuUserDetails details = (WoniuUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        student.setUserId(details.getUserId());
        String s = ComUtil.nowDate();
        student.setCreateTime(s);
        save(student);
        return true;
    }

    @Override
    public List<StudentDto> getStusByClass(Integer classId) throws StudentException {

        Set<Object> members = rtl.opsForSet().members("woniu:student:class" + classId);
        if (members==null||members.size()<=0) {


            QueryWrapper<WoniuStudent> qw = new QueryWrapper<>();
            qw.eq("class_id", classId);
            List<WoniuStudent> list = list(qw);
            if (list == null || list.size() <= 0)
                throw new StudentException("这个班没有学生");
            List<StudentDto> studentDtos = ComUtil.copyList(list, StudentDto.class);
            rtl.opsForSet().add("woniu:student:class" + classId, studentDtos);

            return studentDtos;
        }else{
            ArrayList a = new ArrayList(members);
            return a;
        }
    }
}
